<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountCustomerService;
use Illuminate\Support\Str;
use Request;
use Illuminate\Http\Request as R;

class WechatOffiaccountCustomerServiceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '客服管理';
	
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountCustomerService());
        
        $grid->column('kf_account', '客服帐号');
        $grid->column('kf_nick', '客服昵称');
        $grid->column('kf_id', '客服编号');
        $grid->column('kf_headimgurl', '客服头像')->image();
        $grid->column('kf_wx', '客服人员微信号')->display(function($kf_wx,$column){
            if ($kf_wx) {
                return $kf_wx;
            }
        
            // 否则显示为editable
            return $column->bool();
        });
        $grid->column('online', '是否在线')->bool();
        $grid->column('invite_wx', '邀请中的微信号');
        $grid->column('invite_expire_time', '邀请过期时间');
        $grid->column('invite_status', '邀请的状态')->using(['waiting'=>'等待确认','rejected'=>'被拒绝','expired'=>'过期']);
		
		$grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
			$actions->add(new \Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountCustomerService\Invite);
			
        });
		
		
		$grid->header(function ($query) {
        
            return '<meta name="referrer" content="no-referrer">';
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountCustomerService::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountCustomerService());
        if($form->isCreating()){
            $form->text('kf_account', '客服帐号')->help('完整客服帐号，格式为：帐号前缀@公众号微信号，帐号前缀最多10个字符，必须是英文、数字字符或者下划线，后缀为公众号微信号，长度不超过30个字符, 参考文档：https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html#0')->rules('required');
        }else{
            $form->display('kf_account', '客服帐号')->help('完整客服帐号，格式为：帐号前缀@公众号微信号，帐号前缀最多10个字符，必须是英文、数字字符或者下划线，后缀为公众号微信号，长度不超过30个字符, 参考文档：https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html#0')->rules('required');
        }
        $form->text('kf_nick', '客服昵称')->rules('required');
        $form->image('kf_headimgurl', '客服头像')->rules('required');
        
        return $form;
    }
    
	public function destroy($id)
    {
        try {
            collect(explode(',', $id))->filter()->each(function ($id) {
                $model = new WechatOffiaccountCustomerService();
                $model = $model->findOrFail($id);
                
                $model->delete();
            });

            $response = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } catch (\Exception $exception) {
            $response = [
                'status'  => false,
                'message' => $exception->getMessage() ?: trans('admin.delete_failed'),
            ];
        }

        return response()->json($response);
    }
}
